USE [ServerMonitor]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[BlockSQLText]'))
DROP VIEW [dbo].[BlockSQLText]
GO
USE [ServerMonitor]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[BlockSQLText]
AS
  SELECT S.[spid],[blocked],[waitType],[waitTime],DB_NAME([dbID]) AS DBName,TX.[sql_text]
  FROM [ServerMonitor].[dbo].[Blocking] AS S
  INNER JOIN (SELECT [spid], MAX([CapturedTimeStamp]) AS Time
  FROM [ServerMonitor].[dbo].[Blocking]
  GROUP BY [spid]) AS T
  ON S.[spid] = T.[spid] AND S.[CapturedTimeStamp] = T.Time
  INNER JOIN [ServerMonitor].[dbo].[Blocking_SqlText] AS TX
  ON TX.[spid] = S.[spid] AND TX.[Capture_Timestamp] = S.[CapturedTimeStamp]


GO


